package org.linphone.http;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author LD
 * @Time 2019/12/3 16:35
 * @Describe 静默删除应用工具类
 * @Modify
 */
public class UnInstallUtil {

    private static final String TAG = "UnInstallUtil";

    /**
     * 获取应用列表
     *
     * @param context
     * @return
     */
    public static List<PackageInfo> getAllPackageNameFun(Context context) {

        PackageManager pm = context.getPackageManager();
        List<PackageInfo> list = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
        return list;
    }

    /**
     * 删除应用
     *
     * @param context
     * @param packageName
     */
    public static void deleteSilentWithReflection(Context context, String packageName) {
        String[] args = {"pm", "uninstall", packageName};
        String result = apkProcess(args);
        Log.e(TAG, "uninstall log:" + result);
        if (result != null
                && (result.endsWith("Success") || result.endsWith("Success\n"))) {
            Toast.makeText(context, "卸载成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "卸载失败" + result, Toast.LENGTH_LONG).show();
        }
    }

    public static String apkProcess(String[] args) {
        String result = null;
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();

            result = new String(data);
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                result = e.getMessage();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return result;
    }

    /**
     * 强行停止应用
     */
    public static String killProcess(Context context, String packageName) {
        String  result = "关闭成功,请重新启动linphone";
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//        activityManager.forceStopPackage(packageName);
//        PackageManager.class.getMethod("forceStopPackage",)
        //通过反射调用
        try {
            Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
            method.invoke(activityManager, packageName);
        } catch (NoSuchMethodException e) {
            Log.i(TAG, "killProcess: 找不到该方法");
            e.printStackTrace();
            result = "关闭失败:找不到该方法";
            return result;
        } catch (ClassNotFoundException e) {
            Log.i(TAG, "killProcess: 找不到该类");
            e.printStackTrace();
            result = "关闭失败:找不到该方法";
            return result;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.i(TAG, "killProcess: 非法获取异常");
            result = "关闭失败:找不到该方法";
            return result;
        } catch (InvocationTargetException e) {
            Log.i(TAG, "killProcess: 反射异常");
            e.printStackTrace();
            result = "关闭失败:找不到该方法";
            return result;
        }
        return  result ;
    }


}
